package com.curtain.koreyoshi.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by leejunpeng on 2016/4/9.
 */
public class RingView extends View {

    private final  Paint paint;
    private final Context context;

    public void setArc(boolean arc) {
        isArc = arc;
        invalidate();
    }

    private boolean isArc = false;

    public void setBattery(float battery) {
        this.battery = battery;
        invalidate();
    }

    private float battery;

    private int innerColor = Color.BLACK;
    private int outerColor = Color.BLACK;
    private int ringColor = Color.WHITE;
    private int arcColor = Color.GREEN;

    public void setRingInval(int ringInval){
        this.ringInval = ringInval;
        invalidate();
    }

    public void setInnerColor(int innerColor) {
        this.innerColor = innerColor;
        invalidate();
    }

    public void setOuterColor(int outerColor) {
        this.outerColor = outerColor;
        invalidate();
    }

    public void setRingColor(int ringColor) {
        this.ringColor = ringColor;
        invalidate();
    }

    public void setInnerRadius(int innerRadius) {
        this.innerRadius = innerRadius;
        invalidate();
    }

    public void setRingRadius(int ringRadius) {
        this.ringRadius = ringRadius;
        invalidate();
    }

    private int innerRadius;
    private int ringRadius;
    private int ringInval;

    public RingView(Context context) {

        // TODO Auto-generated constructor stub
        this(context, null);
    }

    public RingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.paint = new Paint();
        this.paint.setAntiAlias(true); //消除锯齿
        this.paint.setStyle(Paint.Style.STROKE); //绘制空心圆
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // TODO Auto-generated method stub
        int center = getWidth()/2;
        int innerCircle = innerRadius; //设置内圆半径,15
        int ringWidth = ringRadius; //设置圆环宽度,4
        int centerY = innerCircle+4*ringWidth+3*ringInval;
        //绘制内圆
//
//        this.paint.setColor(innerColor);
//        this.paint.setStrokeWidth(2);
//        canvas.drawCircle(center,centerY, innerCircle, this.paint);
//        canvas.drawCircle(center,centerY, innerCircle+ringWidth+ringInval, this.paint);
//        canvas.drawCircle(center,centerY, innerCircle+2*(ringWidth+ringInval), this.paint);
//        canvas.drawCircle(center,centerY, innerCircle+3*(ringWidth+ringInval), this.paint);
        //绘制圆环
        this.paint.setColor(ringColor);
        this.paint.setStrokeWidth(ringWidth);
        this.paint.setColor(0x55ffffff);
        canvas.drawCircle(center,centerY, innerCircle+ringWidth/2, this.paint);
        this.paint.setColor(0x44ffffff);
        canvas.drawCircle(center,centerY, innerCircle+ringWidth+ringInval+ringWidth/2, this.paint);
        this.paint.setColor(0x33ffffff);
        canvas.drawCircle(center,centerY, innerCircle+2*(ringWidth+ringInval)+ringWidth/2, this.paint);
        this.paint.setColor(0x22ffffff);
        canvas.drawCircle(center,centerY, innerCircle+3*(ringWidth+ringInval)+ringWidth/2, this.paint);

        //绘制外圆
//        this.paint.setColor(outerColor);
//        this.paint.setStrokeWidth(2);
//        canvas.drawCircle(center,centerY, innerCircle+ringWidth, this.paint);
//        canvas.drawCircle(center,centerY, innerCircle+ringWidth+ringWidth+ringInval, this.paint);
//        canvas.drawCircle(center,centerY, innerCircle+ringWidth+2*(ringWidth+ringInval), this.paint);
//        canvas.drawCircle(center,centerY, innerCircle+ringWidth+3*(ringWidth+ringInval), this.paint);

        if (isArc) {
            this.paint.setColor(ringColor);
            this.paint.setTextSize(15);
            this.paint.setStrokeWidth(1);
            Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
            // 转载请注明出处：http://blog.csdn.net/hursing
            int baseline = (2* centerY  - fontMetrics.bottom - fontMetrics.top) / 2;
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText(String.valueOf((int)(battery *100)),center,baseline,paint);
            float x = innerCircle+ringWidth/2;
            RectF oval = new RectF(center - x, centerY - x, center + x, centerY + x);
            this.paint.setColor(arcColor);
            this.paint.setStrokeWidth(ringWidth);
            canvas.drawArc(oval,360* battery,360* battery,false, paint);

        }

    }
}
